const fs = require('fs')
const path = require('path')
const chalk = require('chalk')

const { NodeSSH } = require('node-ssh')

const ssh = new NodeSSH()

ssh.connect({
  username: 'root',
  password: 'NDqQn*AWEU%QNz!x',
  host: '36.7.128.202',
  port: '22'
}).then(res => {
  // ssh.getFile(`${__dirname}/download/run.sh`,'/opt/wxz-app/cywl/mansys/prod/run.sh').then(res=>{
  //     console.log("The File's contents were successfully downloaded")
  // })

  // 查找环境参数
  const envArg = args.find(arg => arg.startsWith('--env='))
  const env = envArg ? envArg.split('=')[1] : 'testing'// 默认环境为 development
  let path = ''
  console.log('env=', env)
  switch (env) {
    case 'production':
      // 生产环境的部署逻辑
      path = '/data/yll/yll-non-custody-personnel-mansys-vue-prod-business-screen'
      console.log('Deploying to production...')
      break
    case 'testing':
      // 测试环境的部署逻辑
      path = '/data/yll/yll-non-custody-personnel-mansys-vue-test-business-sreen'
      console.log('Deploying to testing...')
      break
    default:
      // 默认环境的部署逻辑
      path = '/data/yll/yll-non-custody-personnel-mansys-vue-test-business-sreen'
      console.log('Deploying to development...')
  }

  console.log(chalk.green('开始重启服务器。。。。。。。。'))
  // const path = '/data/yll/yll-non-custody-personnel-mansys-vue-test-business' // 非羁押测试环境

  // const path = '/data/yll/yll-non-custody-personnel-mansys-vue-prod-business' // 非羁押生产环境
  // const path = '/data/wxz/wxz-common-web-landlord-vue-test-base' // 个人用户
  ssh.execCommand('./run.sh --json', { cwd: path }).then(res => {
    console.log('STDOUT: ' + res.stdout)
    console.log('STDERR: ' + res.stderr)
  })
})
